package com.ysxsoft.wdg.adapter.order;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.bean.ZyOrderBean;

import java.util.List;


public class ZyOrderAdapter extends RecyclerView.Adapter<ZyOrderAdapter.MyHolder> {

    public void setOnOrderClickListener(OnOrderClickListener onOrderClickListener) {
        this.onOrderClickListener = onOrderClickListener;
    }

    private OnOrderClickListener onOrderClickListener;


    private List<ZyOrderBean> list;
    private Context context;

    private boolean isManJian = false;//是否是满减

    public ZyOrderAdapter(List<ZyOrderBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setData(List<ZyOrderBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setStatus(boolean isManJian) {
        this.isManJian = isManJian;
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, null); //解决条目显示不全
        final MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        ZyOrderBean item = list.get(position);

        ZyOrderInnerAdapter adapter = new ZyOrderInnerAdapter(item.getDetail(), context);
        adapter.setStatus(isManJian);
        holder.recyclerView.setAdapter(adapter);

        holder.tvNo.setText("订单号：" + item.getDsn());
        holder.tvStatus.setText(item.getStatu());

        holder.tvMoneyH.setText(context.getString(R.string.price_rmb, item.getAccount()));
        holder.tvNumH.setText("共" + item.getTotal_num() + "件商品   实付");

        TextView btnLeft = holder.btnLeft;
        TextView btnRight = holder.btnRight;
        String textLeft = "取消订单", textRight = "立即支付";

        final int status = item.getStatus();
        switch (status) {//0未付
            case 0://
                textLeft = "取消订单";
                textRight = "立即支付";
                break;
            case 1://1已付，待发货
                textLeft = "退款";
                textRight = "提醒发货";
                break;
            case 2://2已发货，待收货
                textLeft = "查看物流";
                textRight = "确认收货";
                break;
            case 3://3已收货，待评价
                textLeft = "查看物流";
                textRight = "立即评价";
                break;
            case 4://4完成
                textLeft = "删除订单";
                break;
            case 5://5退款售后
                ZyOrderBean.Back back=item.getBack();
                StringBuffer tips=new StringBuffer();
                if("1".equals(back.getB_type())){
                    tips.append("仅退款，");
                }else{
                    tips.append("退货退款，");
                }
                if("0".equals(back.getB_status())){//退款中
                    tips.append("待审核");
                    holder.tvTui.setText("退款中");
                }else if("1".equals(back.getB_status())){//退款成功
                    tips.append("退款成功");
                    holder.tvTui.setText("退款成功");
                }else if("2".equals(back.getB_status())){//退款失败
                    tips.append("退款失败");
                    holder.tvTui.setText("退款失败");
                }
                holder.tvTui.setText(tips.toString());
                textRight = "查看详情";
                break;
            case 6://6退款成功
                holder.tvTui.setText("退款失败");
                textRight = "查看详情";
                break;
        }

        btnLeft.setText(textLeft);
        btnRight.setText(textRight);

        btnRight.setVisibility(status == 4 ? View.GONE : View.VISIBLE);

        holder.tvTui.setVisibility((status == 5 || status == 6) ? View.VISIBLE : View.GONE);
        btnLeft.setVisibility((status == 5 || status == 6) ? View.GONE : View.VISIBLE);

        if (status == 3) {
            if ("1".equals(item.getDiscuss())) {//评价过的不显示按钮
                btnRight.setVisibility(View.GONE);
                holder.tvStatus.setText("已评价");
            } else {
                btnRight.setVisibility(View.VISIBLE);
                holder.tvStatus.setText("待评价");
            }
        }

        btnLeft.setVisibility(status==4?View.GONE:View.VISIBLE);


        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOrderClickListener.OnLeftClickListener(status, position);
            }
        });
        btnRight.setOnClickListener(new View.OnClickListener() {//1提醒发货
            @Override
            public void onClick(View v) {
                onOrderClickListener.OnRightClickListener(status, position);
            }
        });

        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOrderClickListener.OnItemClickListener(position);
            }
        });

        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return holder.rlItem.onTouchEvent(event);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlItem;
        private RecyclerView recyclerView;
        private TextView tvNo, tvStatus, tvMoneyH, tvNumH, tvTui;
        private TextView btnLeft, btnRight;

        public MyHolder(View itemView) {
            super(itemView);


            rlItem = itemView.findViewById(R.id.rl_item);

            tvNo = itemView.findViewById(R.id.tv_no);
            tvStatus = itemView.findViewById(R.id.tv_status);


            recyclerView = itemView.findViewById(R.id.recyclerview);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setNestedScrollingEnabled(false);
            recyclerView.setFocusable(false);

            tvMoneyH = itemView.findViewById(R.id.tv_money_all);
            tvNumH = itemView.findViewById(R.id.tv_num_all);

            tvTui = itemView.findViewById(R.id.tv_tui);

            btnLeft = itemView.findViewById(R.id.tv_l);
            btnRight = itemView.findViewById(R.id.tv_r);
        }
    }
}
